package com.liu.notice.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @Author Liu Ke
 * @Date 2020/8/1  16:19
 * @Description 邮件发送
 * @Version 1.0
 */
@Slf4j
@Service
public class EmailSender {

	@Value("${spring.mail.username}")
	private String from;
	@Autowired
	private JavaMailSender javaMailSender;

	public int sendMail(Mail mail) {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setTo(mail.getTo());
		simpleMailMessage.setSubject(mail.getSubject());
		simpleMailMessage.setText(mail.getContent());
		simpleMailMessage.setFrom(from);
		try {
			javaMailSender.send(simpleMailMessage);
			return 0;
		} catch (Exception e) {
			log.info("@@@@@邮件发送失败！to={},subject={}", mail.getTo(), mail.getSubject());
			return 1;
		}
	}
}
